(function (aopc) {
    aopc.addExpandPage(815, function (get) {
        const PageCommon = get('UtilsCommon', 'PageCommon');

        return {
            expandInit() {
                this.inkCanvas1 = this.getProperty('inkCanvas1');
                this.top_buttons = this.getProperty('top_buttons');
                this.botton_buttons = this.getProperty('botton_buttons');

            },
            expandDataLoad({ data }) {
                if (data.MeetingId) {
                    this.entityData = data;
                    if (data.SignImageUrl) {
                        this.isSigned = true;
                        this.inkCanvas1.setValue(data.SignImageUrl);
                        this.inkCanvas1.enable = false;
                        const [p1, p2] = this.top_buttons.properties;
                        this.setVisible(p1, false);
                        this.setVisible(p2, false);

                        const [p3, p4] = this.botton_buttons.properties;
                        this.setVisible(p3, false);
                        if (!data.IsEndSign) this.setVisible(p4, true);
                    }
                    else if (data.IsEndSign) {
                        this.inkCanvas1.enable = false;
                        this.inkCanvas1.tipText = "签到已结束";
                        this.inkCanvas1.clear(true);
                        this.isSigned = true;
                        const [p1, p2] = this.top_buttons.properties;
                        this.setVisible(p1, false);
                        this.setVisible(p2, false);
                        const [p3, p4] = this.botton_buttons.properties;
                        this.setVisible(p3, false);
                        this.setVisible(p4, false);
                    }
                }
            },
            setVisible(p, v) {
                p.isVisible = v;
                p.setIsVisible && p.setIsVisible(v);
            },
            clear() {
                this.inkCanvas1.clear();
            },
            undo() {
                this.inkCanvas1.undo();
            },
            saveUserSign({ property }) {
                if (this.isLoading || !this.entityData) return;

                if (this.isSigned) {
                    this.replacePage('/meetingSubjectList.html?MeetingId=' + this.pageData.MeetingId);
                    return;
                }

                const img = this.inkCanvas1.getImageFile();
                if (!img) {
                    this.alert('请先签名！');
                    return;
                }

                this.isLoading = true;

                const formData = new FormData();
                formData.append('file', img, img.name);
                formData.append("dirName", "usersign");

                const { serviceName, actionName } = property.serviceInterface;

                PageCommon.showLoading('正在上传，请稍后...');

                this.dispatchAction(serviceName, actionName, { formData }).then(res => {
                    if (this.isSuccessProps(res)) {
                        this.saveUserSign1(res.FileUrl, property);
                    }
                });
            },
            saveUserSign1(imgUrl, property) {
                const { serviceName, actionName } = property.saveServiceInterface;

                const { MeetingId, UserName } = this.entityData;

                const payload = { MeetingUserSign: { SignImageUrl: imgUrl, MeetingId, UserName } }

                this.dispatchAction(serviceName, actionName, payload).then(res => {
                    if (res.isSuccess !== false) {
                        if (this.pageData.isMeeting) this.toBack();
                        else this.replacePage('/meetingSubjectList.html?MeetingId=' + MeetingId);
                    }
                    else if (res.message === '签到已结束') {
                        setTimeout(()=>{
                            this.replacePage('/meetingSubjectList.html?MeetingId=' + MeetingId);
                        }, 2000);
                    }
                    PageCommon.hideLoading();

                    this.isLoading = false;
                });
            },
            reSign({ property }) {
                if (this.isLoading) return;

                this.isLoading = true;

                const { serviceName, actionName } = property.serviceInterface;

                const { UserSignId } = this.entityData;

                const payload = { url: `MeetingUserSign(${UserSignId})` };

                this.dispatchAction(serviceName, actionName, payload).then(res => {
                    if (res.isSuccess !== false) {
                        this.isSigned = false;

                        this.inkCanvas1.clear(true);
                        const [p1, p2] = this.top_buttons.properties;
                        this.setVisible(p1, false);
                        this.setVisible(p2, true);

                        const [p3, p4] = this.botton_buttons.properties;
                        this.setVisible(p3, true);
                        this.setVisible(p4, false);
                    }

                    this.isLoading = false;
                });
            },
            expandGetMeetingData({ entityData }) {
                if (this.pageData.isStart === 'true') entityData.queryString = "IsStart=true";
                return entityData;
            }
        }
    });
})(window.aopc);